package com.moon.robot.listener;

import com.forte.qqrobot.anno.Listen;
import com.forte.qqrobot.anno.depend.Beans;
import com.forte.qqrobot.anno.depend.Depend;
import com.forte.qqrobot.beans.messages.msgget.GroupMsg;
import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import com.forte.qqrobot.sender.MsgSender;
import com.moon.robot.RobotApplication;
import com.moon.robot.Utils.Utils;
import com.moon.robot.pojo.Reply;
import com.moon.robot.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Random;
@Beans
@Service
public class AllListener {
    @Autowired
    private GroupService groupService;

    private static Random random = new Random();


    @Listen(value = MsgGetTypes.groupMsg, sort = 1000)
    public void groupMsg(GroupMsg groupMsg, MsgSender msgSender) {
        if (!RobotApplication.groupcode){
            return;
        }
        List<Reply> vaguereply = groupService.exactSelectMsg(groupMsg.getMsg());
        if (vaguereply!=null&&vaguereply.size()>0){
            Reply reply = vaguereply.get(random.nextInt(vaguereply.size()));
            msgSender.SENDER.sendGroupMsg(groupMsg.getGroup(),reply.getAnswer());
            return;
        }


        Reply reply = Utils.containsWordsAhoCorasick(groupMsg.getMsg(), RobotApplication.VagueReplyList);
        if (reply!=null){
            String answer= groupService.selectAnswerById(reply.getId());
            msgSender.SENDER.sendGroupMsg(groupMsg.getGroup(),answer);
        }

    }
}
